# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=uncrustify
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.77.1
pkgrel=1
pkgdesc="Source Code Beautifier for C, C++, C#, ObjectiveC, D, Java, Pawn and VALA (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://uncrustify.sourceforge.io/"
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://github.com/uncrustify/uncrustify/archive/${_realname}-${pkgver}.tar.gz"
        001-fix-building-with-gcc-13.patch)
sha256sums=('414bbc9f7860eb18a53074f9af14ed04638a633b2216a73f2629291300d37c1b'
            'ab2b3fa5debcbde9f7c44550910b78465fda819d27fca7028aa024e4be52d1ea')

prepare() {
  cd "${srcdir}/${_realname}-${_realname}-${pkgver}"
  # https://github.com/uncrustify/uncrustify/pull/4014
  patch -p1 -i "${srcdir}"/001-fix-building-with-gcc-13.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    "../${_realname}-${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  cd "${srcdir}/${_realname}-${_realname}-${pkgver}"
  install -d -m755 "$pkgdir"/${MINGW_PREFIX}/share/uncrustify
  install -m644 etc/*.cfg "$pkgdir"/${MINGW_PREFIX}/share/uncrustify
}
